package edu.cth.ai23;
public abstract class Solver {

    protected long timeStarted;
    protected long timeFinished;
    protected boolean solved;
    protected boolean stop;
    protected String report;

    protected IProblem problem;

    protected static final Object lock = new Object();


    public long getRunningTime() {
        long runningTime = 0;
        if (this.solved || this.stop)
            runningTime = this.timeFinished - this.timeStarted;
        else
            runningTime = System.currentTimeMillis()-this.timeStarted;

        return runningTime;
    }

    public boolean isSolving() {
        boolean isSolved;

        synchronized (lock) {
            isSolved = solved;
        }

        return !isSolved;
    }

    public Solver() {
        solved = true;
        long currentTime = System.currentTimeMillis();
        timeStarted = currentTime;
        timeFinished = currentTime;
        this.report = "";
    }

    public void updateProblem(IProblem problem) {
        this.stop = false;
        this.report = "";

        if (problem instanceof RubiksCube)
            this.problem = new RubiksCube();
        else if (problem instanceof EightPuzzle)
            this.problem = new EightPuzzle();
        else if (problem instanceof EightPuzzle)
            this.problem = new EightPuzzle();

        this.problem.copyValues(problem);
    }

    public IProblem getProblem() {
        return this.problem;
    }

    public String getReport() {
        return this.report;
    }

    public void stop() { //does not need lock?
        if (!this.solved) {
            this.stop = true;
            this.timeFinished = System.currentTimeMillis();
        }
    }

    public boolean isStoped() {
        return this.stop;
    }

}